package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpDto;
import com.itheima.pojo.LoginInfo;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import com.itheima.utils.LoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public LoginInfo login(EmpDto empDto) {
        Emp emp = empMapper.logList(empDto.getUsername(), empDto.getPassword());

        if (emp == null) {
            throw new LoginException("用户名或密码错误");
        }
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId(emp.getId());
        loginInfo.setName(emp.getName());
        loginInfo.setUsername(emp.getUsername());
        Map map = new HashMap();
        map.put("userId",emp.getId());
        map.put("username",emp.getUsername());
        String token = JwtUtils.generateJwt(map);
        loginInfo.setToken(token);
        return loginInfo;
    }
}
